#!/usr/bin/python

'''
Licensing.py

Wrapper class for Nuvovis licensing API.

This is not used by the free open source version of Cloud Filer.  It is used in
special builds as an example of a licensed python application. 

'''

PWD = '---paste your development key here---'
APP_NAME = 'Cloud Filer'
LICENSE_SERVER_URL = 'http://localhost:9880'
LICENSE_OK = 0
LICENSE_ERROR = 1
LICENSE_EXPIRED = 2

m_licenseKey = ''
m_licensed = False
m_licensingMsg = ''
m_appName = ''
m_clientName = ''
m_expiryDate = ''
m_isPerpetual = 0

def EncryptionLicensed():
	return True
#        return m_licensed

def GetLicenseKey():
	return m_licenseKey

def GetAppName():
	return m_appName

def GetClientName():
	return m_clientName

def GetExpiryDate():
	return m_expiryDate

def IsPerpetual():
	return m_isPerpetual

def Init(key):
	if len(key) > 0:
		licDLL = ctypes.CDLL(GetFilePath(LICENSING_DLL))
		status = licDLL.init(PWD, key, APP_NAME, LICENSE_SERVER_URL)
		DEBUG('status=' + str(status))
		if (status == LICENSE_OK):
			m_licenseKey = key
			m_licensed = True
			m_licensingMsg = 'Valid license found'
			licDLL.getAppName.restype = ctypes.c_char_p
			m_appName = licDLL.getAppName()
			licDLL.getClientName.restype = ctypes.c_char_p
			m_clientName = licDLL.getClientName()
			licDLL.getExpiryDate.restype = ctypes.c_char_p
			m_expiryDate = licDLL.getExpiryDate()
			licDLL.isPerpetual.restype = ctypes.c_int
			m_isPerpetual = licDLL.isPerpetual()
		elif (status == LICENSE_ERROR):
			licDLL.getLastError.restype = ctypes.c_char_p
			m_licensingMsg = 'License error - ' + licDLL.getLastError()
		elif (status == LICENSE_EXPIRED):
			licDLL.getExpiryDate.restype = ctypes.c_char_p
			m_licensingMsg = 'License expired on ' + licDLL.getExpiryDate()
		else:
			raise Exception('Invalid status from pgslic.init')
	else:
		m_licensingMsg = 'License key not found'

